#define JMBC_INSIDE
#include "tlGroup.h"
#include "id.h"

// Context

#define STORE ID_EXTRA_LARGE_STORE

static TLGroup *store[STORE];

void
tlGroupCreateContext(void)
{
	idClearStore(TLGroup, STORE, store);
}

// Generation / Deletion

static
void
Init(TLGroup *group)
{
}

static
void
Term(TLGroup *group)
{
}

void
tlGenGroups(unsigned int n, unsigned int *groups)
{
	idGenObjects(TLGroup, STORE, store, Init, n, groups);
}

void
tlDeleteGroups(unsigned int n, unsigned int *groups)
{
	idDeleteObjects(store, Term, n, groups);
}

// Load / Unload

void
tlGroupSet(unsigned int id, void *array, unsigned int length)
{
	TLGroup *group;

	group = store[id];

	group->array = array;
	group->length = length;
}

void
tlGroupAllocate(unsigned int id, unsigned int n, unsigned int size)
{
	TLGroup *group;

	group = store[id];

	group->array = malloc(n * size);
	group->length = n;
	group->size = size;
}

void
tlGroupUnload(unsigned int id)
{
	TLGroup *group;

	group = store[id];

	free(group->array);
}

void
tlGroupsUnload(unsigned int n, unsigned int *groups)
{
	unsigned int i;

	for(i = 0; i < n; i++)
		tlGroupUnload(groups[i]);
}

// Query

void *
tlGroupArray(unsigned int id)
{
	TLGroup *group;

	group = store[id];

	return group->array;
}

unsigned int
tlGroupLength(unsigned int id)
{
	TLGroup *group;

	group = store[id];

	return group->length;
}


// Disk I/O

void
tlGroupRead(unsigned int id, unsigned int n, unsigned int size, FILE *stream)
{
	TLGroup *group;

	group = store[id];

	group->array = malloc(n * size);
	group->length = n;
	group->size = size;

	fread(group->array, group->length, group->size, stream);
}

void
tlGroupWrite(unsigned int id, FILE *stream)
{
	TLGroup *group;

	group = store[id];

	fwrite(group->array, group->length, group->size, stream);
}
